package main

/*
#include <windows.h>
#cgo LDFLAGS: -lkernel32
*/
import "C"
import (
	"fmt"
	"unsafe"
)

func main() {
	// 定义一个简单的PE文件头
	var peHeader = struct {
		Machine56              uint16
		NumberOfSections16     uint16
		TimeDateStamp32        uint32
		PointerToSymbolTable32 uint32
		NumberOfSymbols32      uint32
		SizeOfOptionalHeader32 uint16
		Characteristics16      uint16
	}{
		Machine56:              0x14c, // I386
		NumberOfSections16:     1,
		TimeDateStamp32:        0,
		PointerToSymbolTable32: 0,
		NumberOfSymbols32:      0,
		SizeOfOptionalHeader32: 0xe0,
		Characteristics16:      0x10b, // Executable, 32-bit
	}

	// 创建文件映射
	fileHandle := C.CreateFile(
		C.CString("test.exe"),
		C.GENERIC_WRITE,
		C.DWORD(0),
		nil,
		C.CREATE_NEW,
		C.FILE_ATTRIBUTE_NORMAL,
		C.HANDLE(0),
	)
	if fileHandle == C.HANDLE(C.INVALID_HANDLE_VALUE) {
		panic("Failed to create file")
	}
	defer C.CloseHandle(fileHandle)

	// 写入PE头
	var written C.DWORD
	peHeaderPtr := unsafe.Pointer(&peHeader)
	C.WriteFile(fileHandle, peHeaderPtr, C.DWORD(unsafe.Sizeof(peHeader)), &written, nil)

	fmt.Println("PE file created successfully")
}
